const {
  toUrl,
  globalData
} = getApp();
import {
  request
} from "@/utils/request";
Page({
  toUrl,
  data: {
    is_show: false,
    order_id: "",
    coupon_list_id: "", //优惠券id
    detailList: {},
    isPay: false,
    is_integral: null,
    globalData
  },
  handleTab1(e) {
     if (Number(this.data.detailList.ord_info.payable) > Number(this.data.detailList.deduction_money)) {
      wx.showToast({
        title: `超出最大可抵扣金额${this.data.detailList.deduction_money}元`,
        icon:'none'
      })
      return
    }
    let {
      types,items1
    } = e.currentTarget.dataset
    let coupon_list_one_name = this.data.detailList.coupon_list_one.name
    if (types == 2 && coupon_list_one_name == undefined) {
      wx.showToast({
        icon:'none',
        title: '积分不足',
      });
      return
    }
    this.setData({
      is_show: !this.data.is_show,
      is_integral: 1,
      'detailList.coupon_list_one':[],
      coupon_list_id:''
    })
    if (this.data.is_show == false) {
      this.setData({
        is_integral: 0
      })
    }
    this.getDetail()
  },
  getYouHui(e) {
    let {
      url
    } = e.currentTarget.dataset;
    if (this.data.is_show == true) {
      return
    }
    wx.redirectTo({
      url: `${url}`,
    });
  },
  goBack() {
    if (this.data.coupon_list_id == '') {
      wx.navigateBack({
        delta: 1
      })
    } else {
      wx.navigateBack({
        delta: 2
      })
    }
  },
  //获取订单详情
  getDetail() {
    let data = {
      order_id: this.data.order_id,
      coupon_list_id: this.data.coupon_list_id, //优惠券id
      is_integral: this.data.is_integral
    };
    wx.showLoading();
    request("Carapi/getOrderinfo", {
      data
    }).then((res) => {
      wx.hideLoading();
      if (res.errcode == 1) {
        this.setData({
          detailList: res.data,
        });
        if (this.data.is_show == true) {
          this.setData({
            'detailList.coupon_list_one.name':'不可用'
          })
        }
      } else {
        wx.showToast({
          title: res.message,
        });
      }
    });
  },
  //提交订单
  submit() {
    let data = {
      order_id: this.data.order_id,
      coupon_list_id: this.data.coupon_list_id, //优惠券id
      act: 'submit',
      is_integral:this.data.is_integral
    };
    wx.showLoading();
    request("Carapi/getOrderinfo", {
      data
    }).then((res) => {
      wx.hideLoading();
      if (res.errcode == 1) {
        if (res.data.pay_status == 1) {
          wx.redirectTo({
            url: `/pages/order/status/status?order_sn=${res.data.order_sn}&carType=${true}`,
          })
        } else {
          this.setData({
            'detailList.ord_info.orderNo': res.data.order_sn
          })
          this.submit2()
        }

      } else {
        wx.showToast({
          title: res.message,
        });
      }
    });

    return


  },
  submit2() {
    const that = this;
    const data = {
      coupon_list_id: this.data.coupon_list_id,
      order_sn: this.data.detailList.ord_info.orderNo,
    };
    wx.showLoading({
      title: '加载中...',
      icon:'none',
      mask:true,
    })
    request("Payment/pay", {
      data
    }).then((res) => {
      console.log(res);
      wx.requestPayment({
        appId: res.data.appId,
        timeStamp: res.data.timeStamp,
        nonceStr: res.data.nonceStr,
        package: res.data.package,
        signType: res.data.signType,
        paySign: res.data.paySign,
        success: function (res) {
          if (res.errMsg == "requestPayment:ok") {
            wx.showToast({
              title: "支付成功",
            });
            this.setData({
              isPay: true
            })
          } else {
            wx.showToast({
              title: "支付失败",
            });

            setTimeout(() => {
              wx.navigateBack({
                delta: 1,
              })
            }, 2000);
          }
          wx.hideLoading()

        },
        fail: function (res) {
          wx.hideLoading()
        }
      });
    });
  },
  onLoad(options) {
    let {
      type,
      coupon_list_id = "",
      is_integral = 0
    } = options; //type就是 order_id
    this.setData({
      order_id: type,
      coupon_list_id,
      is_integral
    });
    this.getDetail();
  },

});